(function($, doc){    
    var _defaultLoadingIconPath = ujs.getRes('images/common16/loading.gif');     
    /**
     * 抽象带有重操作的按钮.
     * @param {Object} cfg
     * {
     *     btnSelector : '#btn',
     *     containerSelector : 'body', 
     *     onclick : function(){},
     *     loadingText : ''
     * }
     */
    function _HeavyButton( cfg ){
        var _this = this;    
        _this._loadingIconPath = _defaultLoadingIconPath;
        
        _this._triggerType = cfg['triggerType'] || 'click'; 
        if( _this._triggerType === 'hover' ){
            _this._triggerType = 'mouseover';
            _this._triggerFunName = 'onMouseover';
        } else {
            _this._triggerFunName = 'onClick';
        }
        
        if(cfg['containerSelector']){
            _this.$buttonContainer = $(cfg['containerSelector']);
        } else {
            _this.$button = $(cfg['btnSelector']);
        }               
        if(cfg['loadingText'] === undefined){
            cfg['loadingText'] = 'loading ...';
        }
        _this.loadingText = cfg['loadingText'];
        
        var _triggerFunction = function($button){            
            if(!_this.isLoading($button)){
                _this.loading($button);
                if( cfg[_this._triggerFunName] ) {
                    cfg[_this._triggerFunName].call( _this, $button || _this.$button );
                }
            }
        };
        if(_this.$buttonContainer){
            _this.$buttonContainer.delegate( cfg['btnSelector'], _this._triggerType, function(){
                _triggerFunction($(this));      
                return false;         
            } );     
        } else {
            _this.$button.bind(_this._triggerType, function(){ 
                _triggerFunction();
                return false;  
            });
        }        
    }
    /**
     * 可以做为方法单独执行.
     */
    _HeavyButton.prototype.loading = function($button, cfg){
        cfg = cfg || {};        
        $button = $button ? $($button) : this.$button;
        $button.attr('isLoading', true);
        $button.prop('disabled', true);
        var $buttonImg = $button.find('>img');        
        if($buttonImg.size()){    //图片Button.
            $button.attr( 'xtitle', $button.attr('title') );                        
            $button.attr( 'title', cfg['loadingText'] || this.loadingText );
            $buttonImg.attr('xsrc', $buttonImg.attr('src') );
            $buttonImg.attr( 'src', $.isEmptyObject(cfg) ? this._loadingIconPath : _defaultLoadingIconPath );
        } else {    //普通Button.
            $button.addClass('loading');
            $button.attr( 'xtext', $button.text() );
            $button.text( cfg['loadingText'] || this.loadingText );                
        }        
    };
    /**
     * 可以做为方法单独执行.
     */
    _HeavyButton.prototype.restore = function($button){                
        $button = $button ? $($button) : this.$button;    
        if($button.attr('isLoading') !== 'true'){
            return;
        }
        $button.attr('isLoading', false);        
        $button.prop('disabled', false);
        var $buttonImg = $button.find('>img');
        if($buttonImg.size()){    //图片Button.
            $button.attr( 'title', $button.attr('xtitle') );
            $buttonImg.attr( 'src', $buttonImg.attr('xsrc') );
        } else {    //普通Button.            
            $button.removeClass('loading');
            $button.text( $button.attr('xtext') );
        }
    };
    _HeavyButton.prototype.isLoading = function($button){
        $button = $button || this.$button;
        return $button.attr('isLoading') == 'true';    
    };
    _HeavyButton.prototype.setLoadingIconPath = function(path){
        if(path){
            this._loadingIconPath = path;
        }
    };
    
    //----------------------------------------------------------------------------------
    
    _HeavyButton.regLoading = function($button, cfg){
        _HeavyButton.prototype.loading($button, cfg);
    };
    
    _HeavyButton.restore = function($button){
        _HeavyButton.prototype.restore($button);
    };
    
    //----------------------------------------------------------------------------------

    ujs.define('HeavyButton', 'ujs.ui', function(){
        return _HeavyButton;
    });

    /**
     $('div button').each(function(){
        var $button = $(this);
        new HeavyButton({
            btnSelector : $button,
            onclick : function(){
                var _this = this;            
                $.ajax({
                    processUrl : false,
                    type : $button.attr('method') || 'get',                
                    url : $button.attr('api'),
                    success : function(responseText){
                        _this.restore();
                        bubble.alert(JSON.stringify( responseText ) );
                    }
                });
            }
        });
    })
    */    
})($, document);